package com.coderman.generateSqlBatchTools.utils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${fanchunshuai} on 2017-4-12.
 *
 * @version: V1.0
 * @Desc: 从文件格式中获取数据，并批量插入表格中
 * 场景适用于带规律的数据文件无法手动导入，并且数据量较大的情况下
 * 用java代码导入数据
 * @Copyright (c) 2017 58到家-版权所有
 */
public class ImportDataFromFileUtils {



    public ImportDataFromFileUtils(String dbConfigPath){
        try {
            JdbcUtils.initDBConnection(dbConfigPath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 读取数据文件，连接数据库，生成插入语句
     * @param filePath  数据文件路径
     * @param splitStr  对单行数据进行分割的标识符
     * @param tableName 插入的表名
     * @return
     */
    public  boolean importDataFromFile(String filePath,String splitStr,String tableName) throws IOException {
        if(StringUtils.isBlank(filePath)){
            System.out.println("filePath is null ,please check param list.... ");
            return false;
        }
        if(StringUtils.isBlank(splitStr)){
            System.out.println("splitStr is null ,please check param list.... ");
            return false;
        }
        if(StringUtils.isBlank(tableName)){
            System.out.println("tableName is null ,please check param list.... ");
            return false;
        }

        File file = new File(filePath);
        if(!file.exists()){
            System.out.println("file is not exist,please check the file path.... ");
            return false;
        }
        final int[] count = {0};

        List<String> list =  FileUtils.readLines(file,"UTF-8");
        final List<String>[] sqlList = new List[]{new ArrayList<String>()};
        list.stream().forEach(strData ->{

            StringBuilder builder = new StringBuilder("insert into ");
            builder.append(tableName);
            builder.append(" values (");
            String [] array = strData.split(splitStr);
            builder.append(array[0]);
            builder.append(",");
            builder.append(array[1]);
            builder.append(",");
            builder.append(array[2]);
            builder.append(",");
            builder.append(array[3]);
            builder.append(")");
            sqlList[0].add(builder.toString());
            if(count[0] % 10000 == 0){
                try {
                    JdbcUtils.exeBatchSql(sqlList[0]);
                } catch (Exception e) {
                    e.printStackTrace();
                    return;
                }
                sqlList[0] = new ArrayList<String>();
            }
            count[0]++;
        });


        return true;
    }




    public static void main(String[] args) {
        try {
            ImportDataFromFileUtils utils = new ImportDataFromFileUtils("E:\\djworkspace\\openResource\\generateSqlBatchFromExcel\\generatesqlbatchfromexcel\\sqlBatchTools\\src\\main\\resources\\jdbc.properties");

            utils.importDataFromFile("C:\\Users\\58\\Desktop\\ratings.txt","::","ratings");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
